<?php
namespace jy_mareafee;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class member extends \plugin{
	
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('区域代理会员注册失败，缺少会员ID！');
		}
		$ismid=$this->get('member',array('mid'=>$member['mid'],'merchid'=>$_SESSION['onemerchid']),array('mid'));
		if($ismid){
			return false;
		}
		$member['merchid']=$_SESSION['onemerchid'];
		$this->insert('member',$member);
	}
	/*
		申请成为区域代理
	*/
	public function apply_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'area_code'=>$area_code,'merchid'=>$_SESSION['onemerchid']),'id');
		if($check){
			exi('您已经代理了该地区，请勿重复代理！','error');
		}
		if($this->setting['agent_type']==1){
			//独家代理模式下，不允许重复
			$check=$this->getcolumn('agent_area',array('uniacid'=>$_SESSION['uniacid'],'area_code'=>$area_code,'merchid'=>$_SESSION['onemerchid']),'id');
			if($check){
				exi('该区域已存在代理！请更换！','error');
			}
		}
		if($this->setting['agent_many']==0){
			//不支持代理多个地区的情况下，此处检查
			$agent=$this->getcolumn('agent_area',array('mid'=>$mid,'merchid'=>$_SESSION['onemerchid']),'id');
			if($agent){
				$this->update('agent_area',array('area_code'=>$area_code,'area'=>ijson_encode($post['area'])),array('merchid'=>$_SESSION['onemerchid'],'mid'=>$mid));
				//exi('您已经是代理商了，不能重复申请！','error');
			}
		}
		$new_apply=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$_SESSION['onemerchid'],
			'mid'=>$mid,
			'status'=>0,
			'is_duodai'=>$post['is_duodai'],
			'duodai'=>$post['duodai'],
			'area_type'=>pdo_getcolumn('core_area',array('code'=>$area_code),'type'),
			'area_code'=>$area_code,
			'area'=>ijson_encode($post['area']),
			'createtime'=>TIMESTAMP,
		);
		$this->insert('agent_area',$new_apply);
		$id=$this->insertid();
		return $id;
	}
	public function up_agent($mid,$area_code,$post=array()){
		if(!$area_code && !$post['is_duodai']){
			exi('获取区域code失败！','error');
		}
		$new_apply=array();
		if($area_code && !$post['is_duodai']){
			$new_apply['area_code']	=$area_code;
			$new_apply['area_type']	=pdo_getcolumn('core_area',array('code'=>$area_code),'type');
		}
		if($post['is_duodai']){
			$new_apply['is_duodai']	=$post['is_duodai'];
			$new_apply['duodai']	=$post['duodai'];
		}
		$this->update('agent_area',$new_apply,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		return true;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark='',$onemerchid=0){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('agent_area',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
			//审核通过，如果会员还不是代理商，此处修改
			$this->update('member',array('is_agent'=>1),array('mid'=>$agent_area['mid'],'is_agent'=>0,'merchid'=>$onemerchid));
		}else{
			$update['remark']=$remark;
		}
		$this->update('agent_area',$update,array('id'=>$agent_id));
		if($update['status']==1){
			$this->model('notice')->notice_fenxiao_add($agent_area['mid']);
		}
		return true;
	}
	public function set_parent($mid,$fromuser){
		$this->update('member',array('agent_id'=>$fromuser),array('mid'=>$mid,'merchid'=>$_SESSION['onemerchid']));
		$this->model('notice')->notice_fenxiao_xiajiadd($mid,$fromuser);
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('member',array('stop'=>1),array('mid'=>$agent_id,'merchid'=>$_SESSION['onemerchid']));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('member',array('stop'=>0),array('mid'=>$agent_id,'merchid'=>$_SESSION['onemerchid']));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($agent_id,$my=false,$merchid=0){
		//
		$agent=$this->get('agent_area',array('id'=>$agent_id));
		if($agent['status']!=1){
			return;
		}
		if($merchid){
			$_SESSION['onemerchid']=$merchid;
			$this->setting=$this->plugin_setting('setting'.$merchid);
		}
		$member=$this->get('member',array('mid'=>$agent['mid'],'merchid'=>$_SESSION['onemerchid']));
		if($member['stop']){
			return;
		}
		$level=$this->get('level',array('id'=>$agent['level_id']));
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level['level'],'merchid'=>$_SESSION['onemerchid']),
			'order_by'=>'level asc',
			'limit'=>'1',
		);
		$levels=$this->get_list('level',$parameter);
		$next_level=$levels[0];
		if(!$next_level){
			return;
		}
		$conditions=json_decode($next_level['conditions'],true);
		$level_up_types=$this->model()->current_level_up_types($merchid);//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		$up=true;
		foreach($level_up_types as $type){
			if($agent[$type]<$conditions[$type]){
				$up=false;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('agent_area',$update,array('id'=>$agent_id));
		}
		return true;
	}
}